﻿
$(function () {
    document.addEventListener("WeixinJSBridgeReady", function () {
        weixinJSBgReadyEventSet();
    });
})


function weixinJSBgReadyEventSet() {
    var appid = ""; //经常设置为''
    var title = $("#hfTitle").val();
    var link = $("#hfLink").val() == "" ? window.location.href : $("#hfLink").val();
    var desc = $("#hfDesc").val();
    var img_url = $("#hfImg_Url").val(); ; ;
    var img_width = "120"; //好像设置成其他也没用
    var img_height = "120";
    share(appid, desc, link, desc, img_url, img_width, img_height, 'menu:share:timeline', 'shareTimeline'); //timeline：分享至朋友圈
    share(appid, title, link, desc, img_url, img_width, img_height, 'menu:share:appmessage', 'sendAppMessage'); //appmessage分享至微信好友
}

function share(appid, title, link, desc, img_url, img_width, img_height, button, event) {
    if (typeof WeixinJSBridge == "undefined") {
        alert("请先通过微信分享文章 ");
    } else if (typeof window.WeixinJSBridge.invoke == 'undefined') {
        alert("invoke!");
    }
    else {
        WeixinJSBridge.on(button, function (argv) {
            WeixinJSBridge.invoke(event, {
                "appid": appid,             //设置为null
                "title": title,
                "link": link,
                "desc": desc,
                "img_url": img_url,
                "img_width": img_width,                            //图片宽度
                "img_height": img_height                            //图片高度
            }, function (d) {
                //alert(d.err_msg);//这里是分享功能的回调函数，有错误的时候有用
            });
        });
    }
}


$(".shareWeiXin").click(function () {
   // $("body").append("<div class='alert-bg'></div><div class='share'><img src='images/share_bg.png' /></div>");
    $(".share").css("display", "block");
    $(".alert-bg").css("display", "block");
});

$(".share").on("click", function () {
    $(".alert-bg").css("display", "none");
    $(".share").css("display", "none");
});